//
//  KKComposeToolBar.swift
//  KKWeibo
//
//  Created by kassia on 16/7/21.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit

// 枚举
enum KKComposeToolBarButton: Int {

    case Picture = 0 // 图片
    
    case Mention = 1 // @
    
    case Trend = 2 // 话题
    
    case Emoticon = 3 // 表情
    
    case Add = 4 // 加号
    

}
@available(iOS 9.0, *)
class KKComposeToolBar: UIStackView {

    var selectedComposeToolBarClosuer: ((type:KKComposeToolBarButton)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    private func setupUI() {
    
        // 设置布局方式
        axis = .Horizontal
        // 子控件填充方式
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        
        addChildButton("compose_mentionbutton_background", type: .Mention)
        
        addChildButton("compose_trendbutton_background", type: .Picture)
        
        addChildButton("compose_emoticonbutton_background", type: .Picture)
        
        addChildButton("compose_add_background", type: .Picture)
    
    }
    
    // 添加子控件
    private func addChildButton(imageName: String,type: KKComposeToolBarButton) {
        
       let button = UIButton()
        
        // 获取枚举的原始值
        button.tag = type.rawValue
        
        button.addTarget(self, action: "selectedButtonAction:", forControlEvents: .TouchUpInside)
        // 设置图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        button.adjustsImageWhenHighlighted = false
        
        // 添加控件
        addArrangedSubview(button)
        
    }
    
    
    @objc private func selectedButtonAction(button: UIButton) {
    
        let type = KKComposeToolBarButton(rawValue: button.tag)!
        
        // 执行闭包回调
        selectedComposeToolBarClosuer?(type: type)
    
    }
}
